/*** This do-file plots a binscatter of changes in low-education job postings vs
median 2 bedroom rent.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Jobs"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

project, uses("${root}/data/web_${jobs_2021_2}/data/Job Postings - County - Weekly.csv")
import delimited "${root}/data/web_${jobs_2021_2}/data/Job Postings - County - Weekly.csv", clear

* Create date
gen date = mdy(month, day_endofweek, year)

* Re-format change variable
replace bg_posts_jzgrp12 = bg_posts_jzgrp12 * 100

gegen temp = mean(bg_posts_jzgrp12) if year == 2021 & month == 12, by(countyfips)
gegen change_december_2021 = mean(temp), by(countyfips)
drop temp

* Merge in county-level covariates
rename countyfips county_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge m:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", keep(1 3) nogen keepusing(med_2br_2014_2018_est pop_2014_2018_est medhhinc_2014_2018_est)
rename county_fips countyfips

rename (pop_2014_2018_est med_2br_2014_2018_est medhhinc_2014_2018_est) ///
	   (pop_2018 median_2br_rent_2018 medhhinc_2018)

gcollapse change_* median_2br_rent_2018 pop_2018, by(countyfips)

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

reg change_december_2021 median_2br_rent_2018 [w = pop_2018], r
	local beta = _b[median_2br_rent_2018] * 1000
	local beta_december_2021: display %4.2f `beta'
	local std_err = _se[median_2br_rent_2018] * 1000
	local se_december_2021: display %4.2f `std_err'

local unit "/$1000"

binscatter change_december_2021 median_2br_rent_2018 [w = pop_2018] ///
	, ///
	${title_`version'} ///
	color(oi1) lcolor(oi2) ///
	ytitle("Change in Low-Education Job Postings (%)" "from January 2020 to December 2021") ///
	ylab(30 "+30%" 20 "+20%" 10 "+10%" 0 "0%", nogrid) ///
	yscale(range(-5 30)) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	xsize(10) ///
	xsc(ra(500 2050)) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	text(-5 500 "Slope = `beta_december_2021'%`unit' (s.e. = `se_december_2021')", place(ne) just(left) color(gs8) size(medlarge)) ///
	legend(off)
	oi_graph_export "${root}/results/Jobs/Low-Educ Job Postings vs Rent - December 2021", type(${fig_type})

* Black and white version for QJE
binscatter change_december_2021 median_2br_rent_2018 [w = pop_2018] ///
	, ///
	${title_`version'} ///
	color(gs0) lcolor(gs8) ///
	ytitle("Change in Low-Education Job Postings (%)" "from January 2020 to December 2021") ///
	ylab(30 "+30%" 20 "+20%" 10 "+10%" 0 "0%", nogrid) ///
	yscale(range(-5 30)) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	xsize(10) ///
	xsc(ra(500 2050)) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	text(-5 500 "Slope = `beta_december_2021'%`unit' (s.e. = `se_december_2021')", place(ne) just(left) color(gs8) size(medlarge)) ///
	legend(off)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_6a.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_6a.svg")
